﻿using AudioStudio;
using AudioStudio.Components;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WorldMapBase.Camera;

/// <summary>
/// 沙盘背景音乐控制
/// </summary>
public enum BackMusicType
{
    NONE = 0,
    NORMAL_STATE = 1,   //正常状态下的背景音乐
    BATTLE_STATE = 2,   //战斗状态下的背景音乐
}

/// <summary>
/// 沙盘音乐管理(暂时背景)
/// </summary>
public class WorldAudioController
{
    #region 背景音乐部分
    /// <summary>
    /// 当前正在播放的背景音乐类型
    /// </summary>
    private BackMusicType currentWorldBackMusic;
    /// <summary>
    /// 战斗背景音乐计数器(用于判断啥时候结束,全局限制成一个)
    /// </summary>
    private int backBattleMusicCounter;

    /// <summary>
    /// 绑定的用于接受声音的物体
    /// </summary>
    GameObject bindMusicListner;

    #endregion

    public WorldAudioController(ICamera sandCamera)
    {
        backBattleMusicCounter = 0;
        currentWorldBackMusic = BackMusicType.NONE;
        bindMusicListner = GlobalAudioEmitter.GameObject;
    }

    /// <summary>
    /// 播放沙盘的背景音乐
    /// 当前状态切当前状态,不截断
    /// </summary>
    public void PlaySandBackMusic(BackMusicType backMusicType)
    {
        string musicName = GetSoundMusicName(backMusicType);
        if (musicName == "") return;

        //None->播放(第一次播放)
        if (currentWorldBackMusic == BackMusicType.NONE && backMusicType != BackMusicType.NONE)
        {
            if(backMusicType == BackMusicType.BATTLE_STATE)
            {
                backBattleMusicCounter++;
                AudioManager.PlayMusic(musicName, bindMusicListner);
            }else if(backMusicType == BackMusicType.NORMAL_STATE)
            {
                AudioManager.PlayMusic(musicName, bindMusicListner);
            }
            currentWorldBackMusic = backMusicType;
        }
        else if(currentWorldBackMusic == BackMusicType.NORMAL_STATE)
        {
            NormalToNext(backMusicType);
        }else if(currentWorldBackMusic == BackMusicType.BATTLE_STATE)
        {
            BattleToNext(backMusicType);
        }
    }

    #region 转换
    /// <summary>
    /// Normal转到下一个
    /// NORMAL->NORMAL截断
    /// </summary>
    private void NormalToNext(BackMusicType nextMusicType)
    {
        if(nextMusicType == BackMusicType.NORMAL_STATE) return;

        if(nextMusicType == BackMusicType.BATTLE_STATE)
        {
            //AudioManager.StopMusic(bindMusicListner);
            if (backBattleMusicCounter == 0)
            {
                AudioManager.PlayMusic(GetSoundMusicName(nextMusicType), bindMusicListner);
            }
            backBattleMusicCounter++;
            currentWorldBackMusic = BackMusicType.BATTLE_STATE;
        }
        else if(nextMusicType == BackMusicType.NONE)
        {
            //AudioManager.StopMusic(bindMusicListner);
        }
    }

    /// <summary>
    /// Battle转下一个
    /// </summary>
    /// <param name="nextMusicType"></param>
    private void BattleToNext(BackMusicType nextMusicType)
    {
        if (nextMusicType == BackMusicType.BATTLE_STATE)
        {
            //Battle->Battle
            backBattleMusicCounter++;
            return;
        }
        backBattleMusicCounter--;

        if (backBattleMusicCounter <= 0)
        {
            if (nextMusicType == BackMusicType.NORMAL_STATE)
            {
                AudioManager.PlayMusic(GetSoundMusicName(nextMusicType), bindMusicListner);
                currentWorldBackMusic = BackMusicType.NORMAL_STATE;
            }
            backBattleMusicCounter = 0;
        }
    }

    #endregion

    #region 播放音乐
    /// <summary>
    /// 获取对应音乐的event名称
    /// </summary>
    /// <param name="musicType"></param>
    /// <returns></returns>
    private string GetSoundMusicName(BackMusicType musicType)
    {
        switch (musicType)
        {
            case BackMusicType.NORMAL_STATE:
                return "Music_World";
            case BackMusicType.BATTLE_STATE:
                return "Music_World_Fight";
            default:
                return "";
        }
    }
    #endregion

    #region 控制

    public void StopAll()
    {
        backBattleMusicCounter = 0;
    }

    public void Clear()
    {
        StopAll();
        bindMusicListner = null; 
    }

    #endregion
}
